<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-08-28 14:20:28
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 加料单
 */

namespace App\Http\Services\Pro;


use App\Http\Services\Service;
use App\Repositories\ProPotRawSendDetailRepository;
use App\Repositories\ProPotRawSendRepository;

/**
 * @property ProPotRawSendRepository $repository
 * @property ProPotRawSendDetailRepository $SendDetail
 */
class RawSendService extends Service
    {

    public function boot(

        ProPotRawSendRepository $repository, //出库单
        ProPotRawSendDetailRepository $SendDetail, //出库明细

    ) {
        $this->repository = $repository;
        $this->SendDetail = $SendDetail;
        }

    public function list()
        {
        // DB::enableQueryLog();
        $data = $this->repository->condition(request())->getGrid();
        // dd(DB::getQueryLog());

        return $data;
        }
    public function printReport()
        {
        return $this->repository->getAddRawReport($this->getRequestIds());
        }
    public function details()
        {
        $data = $this->SendDetail->condition(request())->getGrid();
        return $data;
        }
    protected function save()
        {
        $request = request();
        // dd($request->details);
        $model = $this->repository->createSend(
            $request->id,
            $request->material_id,
            $request->dyelot,
            $request->color_id,
            $request->quantity,
            array_map(function ($item)
                {
                unset($item['id'], $item['creator_id'], $item['created_at'], $item['updator_id'], $item['updated_at']);
                $item['rate']     = $item['new_rate'];
                $item['quantity'] = $item['new_quantity'];
                $item['cost']     = $item['new_cost'];
                $item['uunit_id'] = $item['ouunit_id'];
                return $item;
                }, $request->details),
            2
        );
        $this->repository->outStorage($model);
        }
    protected function delete()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($model)
            {
            $this->repository->nooutStorage($model);
            //删除单据明细
            $model->details->filter(function ($detail)
                {
                $detail->delete();
                });
            //删除单据
            $model->delete();
            });
        }
    }